-- area_open
-- create by zhuangsl
-- 技能：以目标为中心，开启一定范围内所有格子
-- 目前支持如下配置：
-- 1、area_open(1, 1)：开启以目标为中心，n * n正方形范围的格子，半径为1
-- 2、area_open(2, 1)：开启以目标为中心，距离n以内范围的格子，距离为1

return {
    apply = function(source, target, skillId, round, para, extra)
        local grid;
        local grids = {};
        local radius = para[2];
        
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end
        local coor = DungeonM.convertToCoor(pos);

        if 1 == para[1] then
            -- 第一类范围，n * n
            for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
                local c = DungeonM.convertToCoor(p);

                -- 有效范围内
                local near = math.abs(c.x - coor.x) <= radius and math.abs(c.y - coor.y) <= radius;

                local grid = DungeonM.getGridByPos(p);

                -- 待开启格子
                if  near  then
                    table.insert(grids, grid);
                end
            end
        elseif 2 == para[1] then
            --  第二类范围，距离n
            for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
                local c = DungeonM.convertToCoor(p);

                -- 有效范围内
                local near = math.abs(c.x - coor.x) + math.abs(c.y - coor.y) <= radius;

                local grid = DungeonM.getGridByPos(p);

                -- 待开启格子
                if near then
                    table.insert(grids, grid);
                end
            end
        end

        for _, grid in pairs(grids) do
            if not grid:isOpened() then
                grid:open();
            end
        end

    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), };
    end,
    
    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        return desc;
    end,
};
